package com.jk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.jk.mapper.LXCMapper;
import com.jk.model.*;
import com.jk.service.LXCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Component
public class LXCServiceImpl implements LXCService {

    @Autowired
    private LXCMapper lxcMapper;

    /**
     * 商城导航
     * @return
     */
    @Override
    public List<ProductTree> findProductTree() {
        Integer pid = 0;
        List<ProductTree> trees = findTreeNodes(pid);
        return trees;
    }

    private List<ProductTree> findTreeNodes(Integer pid) {
        List<ProductTree> trees = lxcMapper.findProductTree(pid);
        for (ProductTree ProductTree : trees) {
            Integer id = ProductTree.getId();
            List<ProductTree> treeNodes = findTreeNodes(id);
            if(treeNodes==null || treeNodes.size()<=0) {
                ProductTree.setSelectable(true);
            }else {
                ProductTree.setNodes(treeNodes);
            }
        }
        return trees;
    }

    /**
     * 新增商品
     * @param productBean
     * @param storeId
     */
    @Override
    public void addProudct(ProductBean productBean, Integer storeId) {
        lxcMapper.addProudct(productBean);
        Integer productId = productBean.getId();
        StroeProductBean stroeProductBean = new StroeProductBean();
        stroeProductBean.setProductId(productId);
        stroeProductBean.setStoreId(storeId);
        lxcMapper.addStoreProduct(stroeProductBean);
    }

    /**
     * 商品分页查询
     * @param page
     * @param rows
     * @param productBean
     * @return
     */
    @Override
    public Map findProductList(Integer page, Integer rows, ProductBean productBean,Integer userId) {
        //构建总返回
        HashMap<String, Object> result = new HashMap<>();
        //构建总查询
        HashMap<String, Object> params = new HashMap<>();
        params.put("productBean", productBean);
        params.put("user",userId);
        //查询count
        int count = lxcMapper.findProductCount(params);
        //设置分页
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //查询list
        List<ProductBean> books = lxcMapper.findProductList(params);
        result.put("total", count);
        result.put("rows", books);
        return result;
    }

    /**
     * 商品上下架
     * @param productId
     * @param falg
     */
    @Override
    public void productState(Integer productId, Integer falg) {
        lxcMapper.productState(productId,falg);
    }

    /**
     * 条查：加载商品类型
     * @return
     */
    @Override
    public List<ProductTypeBean> findProductType() {
        return lxcMapper.findProductType();
    }

    /**
     * 删除商品
     * @param productId
     */
    @Override
    public void delProduct(Integer productId) {
        lxcMapper.delProduct(productId);
    }

    /**
     * 库存商品列表
     * @param page
     * @param rows
     * @param userId
     * @return
     */
    @Override
    public Map findProductStock(Integer page, Integer rows, ProductBean productBean, Integer userId) {
        //构建总返回
        HashMap<String, Object> result = new HashMap<>();
        //构建总查询
        HashMap<String, Object> params = new HashMap<>();
        params.put("productBean", productBean);
        params.put("user",userId);
        //查询count
        int count = lxcMapper.findProductStockCount(params);
        //设置分页
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //查询list
        List<ProductBean> books = lxcMapper.findProductStock(params);
        result.put("total", count);
        result.put("rows", books);
        return result;
    }

    /**
     * 库存：商品批量上架
     * @param ids
     */
    @Override
    public void productManyStock(Integer[] ids) {
        lxcMapper.productManyStock(ids);
    }

    /**
     * 店铺：出售中的商品
     * @param page
     * @param rows
     * @param productBean
     * @param userId
     * @return
     */
    @Override
    public Map findStoreProductList(Integer page, Integer rows, ProductBean productBean, Integer userId) {
        //构建总返回
        HashMap<String, Object> result = new HashMap<>();
        //构建总查询
        HashMap<String, Object> params = new HashMap<>();
        params.put("productBean", productBean);
        params.put("user",userId);
        //查询count
        int count = lxcMapper.findStoreProductCount(params);
        //设置分页
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //查询list
        List<ProductBean> books = lxcMapper.findStoreProductList(params);
        result.put("total", count);
        result.put("rows", books);
        return result;
    }

    /**
     * 店铺：下架的商品
     * @param page
     * @param rows
     * @param productBean
     * @param userId
     * @return
     */
    @Override
    public Map findStoreDownProduct(Integer page, Integer rows, ProductBean productBean, Integer userId) {
        //构建总返回
        HashMap<String, Object> result = new HashMap<>();
        //构建总查询
        HashMap<String, Object> params = new HashMap<>();
        params.put("productBean", productBean);
        params.put("user",userId);
        //查询count
        int count = lxcMapper.findStoreDownProductCount(params);
        //设置分页
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //查询list
        List<ProductBean> books = lxcMapper.findStoreDownProductList(params);
        result.put("total", count);
        result.put("rows", books);
        return result;
    }

    /**
     * 审核中的商品
     * @param page
     * @param rows
     * @param productBean
     * @param userId
     * @return
     */
    @Override
    public Map findAuditProduct(Integer page, Integer rows, ProductBean productBean, Integer userId) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> params = new HashMap<>();
        params.put("productBean", productBean);
        params.put("user",userId);
        int count = lxcMapper.findStoreAuditProductCount(params);
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        List<ProductBean> books = lxcMapper.findStoreAuditProductList(params);
        result.put("total", count);
        result.put("rows", books);
        return result;
    }

    /**
     * 查询店铺
     * @return
     */
    @Override
    public List<StoreBean> findStore() {
        return lxcMapper.findStore();
    }

    /**
     * 登录
     * @param userAccount
     * @return
     */
    @Override
    public UserBean findUserInfoByAccount(Integer userAccount) {
        return lxcMapper.findUserInfoByAccount(userAccount);
    }

}
